package cn.learn.controller.baseinfo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.Factory;
import cn.learn.domain.cargo.FactoryExample;
import cn.learn.domain.cargo.Product;
import cn.learn.domain.cargo.ProductExample;
import cn.learn.service.cargo.FactoryService;
import cn.learn.service.cargo.ProductService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/baseinfo/product")
public class ProductController extends BaseController {

    @Reference
    private ProductService productService;
    @Reference
    private FactoryService factoryService;

    @RequestMapping(value = "/list", name = "展示商品列表")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        ProductExample productExample = new ProductExample();
        productExample.setOrderByClause("input_time desc");
        PageInfo products = productService.findAll(productExample, pageNum, pageSize);
        request.setAttribute("pageList", products);
        return "/baseinfo/product/product-list";
    }

    /**
     * 添加商品
     * @param product
     * @return
     */
    @RequestMapping(value = "addOne", name = "添加商品页面")
    public String addOne(Product product) {
        if (request.getMethod().equals("POST")) {
            product.setCreateBy(user.getUserName());
            product.setCreateDept(user.getDeptName());
            productService.save(product);
            return "redirect:/baseinfo/product/list.do";
        }
        FactoryExample factoryExample = new FactoryExample();
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        return "/baseinfo/product/product-add";
    }

    /**
     * 修改商品
     * @param product
     * @return
     */
    @RequestMapping(value = "update", name = "修改商品")
    public String update(Product product) {
        if (request.getMethod().equals("POST")) {
            productService.update(product);
            return "redirect:/baseinfo/product/list.do";
        }
        Product resultProduct = productService.findById(product.getId());
        FactoryExample factoryExample = new FactoryExample();
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("data", resultProduct);
        request.setAttribute("factoryList", factoryList);
        return "/baseinfo/product/product-update";
    }

    /**
     * 删除商品信息
     * @return
     */
    @RequestMapping(value = "delete", name = "删除商品")
    public String delete(String id) {
        productService.delete(id);
        return "redirect:/baseinfo/product/list.do";
    }

    /**
     * 跳转饼
     * @return
     */
    @RequestMapping("/bing")
    public String bing() {

        return "baseinfo/product/product-bing";
    }


    /**
     * 钱的价格的销售情况  返回json数据
     * @return
     */
    @RequestMapping("/findqian")
    @ResponseBody
    public List<Map> findqian() {
        List<Map> findqian = productService.findqian();
        return findqian;
    }

}
